package com.business.user.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.business.common.dto.R;
import com.business.common.enums.ErrorEnum;
import com.business.common.exception.CustomException;
import com.business.redis.ExpiresTimeEnum;
import com.business.redis.constant.RedisKeyConstant;
import com.business.redis.utils.RedisKeysUtil;
import com.business.redis.utils.RedisUtil;
import com.business.user.annotation.TokenPermission;
import com.business.user.dto.UserParams;
import com.business.user.entity.UserConcern;
import com.business.user.entity.UserDetails;
import com.business.user.entity.UserDto;
import com.business.user.service.UserConcernService;
import com.business.user.utils.PageFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 关注表: 后端controller类
 *
 * @author chenjiehong
 * @date 2019-11-29 14:28:27
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/concern")
public class UserConcernController {


    @Autowired
    private UserConcernService userConcernServiceImpl;


    /**
     * 新增关注表
     *
     * @param params
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/add")
    @ResponseBody
    @TokenPermission
    public R add(@RequestBody JSONObject params, UserDto user) {
        Integer concernUserId = params.getInteger("concernUserId");
        UserConcern userConcern = new UserConcern();
        userConcern.setUserId(user.getId());
        userConcern.setConcernUserId(concernUserId);
        UserConcern r = userConcernServiceImpl.add(userConcern);
        String key = String.format(RedisKeyConstant.USER_FRIEND_LIST,user.getId());
        RedisUtil.addSetValue(key, concernUserId);
        return R.success(r);
    }


    /**
     * 删除关注表
     *
     * @param params
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/delete")
    @ResponseBody
    @TokenPermission
    public R delete(@RequestBody JSONObject params, UserDto user) {
        Integer concernUserId = params.getIntValue("concernUserId");
        UserConcern userConcern = new UserConcern();
        userConcern.setUserId(user.getId());
        userConcern.setConcernUserId(concernUserId);
        Integer r = userConcernServiceImpl.delete(userConcern);
        String key = String.format(RedisKeyConstant.USER_FRIEND_LIST,user.getId());
        RedisUtil.removeSetValue(key, concernUserId);
        return R.success(r);
    }


    /**
     * 删除关注表
     *
     * @param params
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/deleteList")
    @ResponseBody
    @TokenPermission
    public R deleteList(@RequestBody JSONObject params, UserDto user) {
        if (!params.containsKey("concernUserIdList")) {
            throw new CustomException(ErrorEnum.PARAMS_LOSE, "concernUserIdList");
        }
        List<Integer> concernUserIdList = params.getJSONArray("concernUserIdList").toJavaList(Integer.class);

        Integer r = userConcernServiceImpl.deleteList(user.getId(), concernUserIdList);
        String key = String.format(RedisKeyConstant.USER_FRIEND_LIST,user.getId());
        if (RedisUtil.hasKey(key)) {
            concernUserIdList.forEach(concernUserId -> {
                RedisUtil.removeSetValue(key, concernUserId);
            });
        }
        return R.success(r);
    }

    /**
     * 查询用户粉丝
     *
     * @date 2019-11-29 14:28:26
     */
    @PostMapping("/queryFansPage")
    @ResponseBody
    @TokenPermission
    public R queryFansPage(@RequestBody JSONObject params, UserDto user) {
        Page<UserDetails> page = new PageFactory<UserDetails>().defaultPage(params);
        UserParams userParams = JSONObject.parseObject(params.toJSONString(), UserParams.class);
        return R.success(userConcernServiceImpl.queryFansPage(page, user.getId(), userParams));
    }

    /**
     * 查询用户关注
     *
     * @date 2019-11-29 14:28:26
     */
    @PostMapping("/queryFriendPage")
    @ResponseBody
    @TokenPermission
    public R queryFriendPage(@RequestBody JSONObject params, UserDto user) {
        Page<UserDetails> page = new PageFactory<UserDetails>().defaultPage(params);
        UserParams userParams = JSONObject.parseObject(params.toJSONString(), UserParams.class);
        return R.success(userConcernServiceImpl.queryFriendPage(page, user.getId(), userParams));
    }

    /**
     * 查询用户关注id列表
     *
     * @date 2019-11-29 14:28:26
     */
    @PostMapping("/queryFriendList")
    @ResponseBody
    @TokenPermission
    public R queryFriendList(@RequestBody JSONObject params, UserDto user) {
        Integer userId = params.getInteger("userId");
        String key = String.format(RedisKeyConstant.USER_FRIEND_LIST,user.getId());
        if (RedisUtil.hasKey(key)) {
            return R.success(RedisUtil.getValue(key));
        } else {
            Set<Integer> list = userConcernServiceImpl.queryFriendList(userId);
            for (Integer id : list) {
                RedisUtil.addSetValue(key, id);
            }
            RedisUtil.expire(key, ExpiresTimeEnum.LEVEL_8.getExpiresTime());
            return R.success(list);
        }

    }


    /**
     * 查询用户列表
     *
     * @date 2019-11-29 14:28:26
     */
    @PostMapping("/queryUserPage")
    @ResponseBody
    @TokenPermission(required = false)
    public R queryUserPage(@RequestBody JSONObject params, UserDto user) {
        Page<UserDetails> page = new PageFactory<UserDetails>().defaultPage(params);
        UserParams userParams = JSONObject.parseObject(params.toJSONString(), UserParams.class);
        return R.success(userConcernServiceImpl.queryUserPage(page, user.getId(), userParams));
    }


    /**
     * 查询推荐用户列表
     *
     * @date 2019-11-29 14:28:26
     */
    @PostMapping("/queryRecommendUserPage")
    @ResponseBody
    @TokenPermission(required = false)
    public R queryRecommendUserPage(@RequestBody JSONObject params, UserDto user) {
        Page<UserDetails> page = new PageFactory<UserDetails>().defaultPage(params);
        return R.success(userConcernServiceImpl.queryRecommendUserPage(page, user.getId()));
    }


}